package com.yuanli.cosmeticmirror.persenter;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.comenjoysoft.baselibrary.presenter.BasePresenter;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.tsy.sdk.pay.alipay.Alipay;
import com.tsy.sdk.pay.weixin.WXPay;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.base.WxConstant;
import com.yuanli.cosmeticmirror.bean.AlipayBean;
import com.yuanli.cosmeticmirror.bean.MemberBean;
import com.yuanli.cosmeticmirror.bean.MemberInfoBean;
import com.yuanli.cosmeticmirror.model.biz.IMemberBiz;
import com.yuanli.cosmeticmirror.model.impl.MemberImpl;
import com.yuanli.cosmeticmirror.model.iview.IMemberView;
import com.yuanli.cosmeticmirror.utils.CommonUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MemberPersenter extends BasePresenter<IMemberView> {
    public static final int MEMBER_PRICES = 0;
    public static final int MEMBER_WECHAT = 1;
    public static final int MEMBER_ALIPAY = 2;
    public static final int MEMBER_VIP = 3;
    private IMemberBiz biz;

    public MemberPersenter(IMemberView view, LifecycleOwner owner) {
        attachView(view);
        biz = new MemberImpl(owner);
    }

    public void getPrices() {
        biz.getPrices(new CallbackListener<List<MemberBean>>() {
            @Override
            public void success(List<MemberBean> object) {
                mView.onSucess(object);
            }

            @Override
            public void failed(Object object) {
                mView.Error(MEMBER_PRICES);
            }
        });
    }

    public void pay(boolean isCheck, String price) {
        HashMap<String, Object> hashMap = new HashMap<>(6);
        hashMap.put("user_phone", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT));
        hashMap.put("commodity", "购买会员");
        hashMap.put("client_id", CommonUtils.getDeviceId(MyApp.getInstance()));
        hashMap.put("price", price);
        hashMap.put("app_name", "化妆镜");
        hashMap.put("phone_type", CommonUtils.getBrand());
        String json = JSON.toJSONString(hashMap);
        biz.pay(json, isCheck, new CallbackListener<AlipayBean>() {
            @Override
            public void success(AlipayBean object) {
                if (isCheck) {
                    doWxPay(object.getPrepay_id());
                } else {
                    doZfbPay(object.getBody());
                }
            }

            @Override
            public void failed(Object object) {
                if (isCheck) {
                    mView.Error(MEMBER_WECHAT);
                } else {
                    mView.Error(MEMBER_ALIPAY);
                }

            }
        });
    }

    /**
     * 支付宝支付
     *
     * @param payParam
     */
    private void doZfbPay(String payParam) {
        new Alipay(mView.getActivity(), payParam, new Alipay.AlipayResultCallBack() {
            @Override
            public void onSuccess() {
                mView.paySucess(MEMBER_ALIPAY);
            }

            @Override
            public void onCancel() {
                mView.Error(MEMBER_ALIPAY);
            }

            @Override
            public void onDealing() {
                mView.showToast("支付处理中...");
            }

            @Override
            public void onError(int errorCode) {
                mView.Error(MEMBER_ALIPAY);
                switch (errorCode) {
                    case Alipay.ERROR_RESULT:
                        mView.showToast("支付失败:支付结果解析错误");
                        break;
                    case Alipay.ERROR_NETWORK:
                        mView.showToast("支付失败:网络连接错误");
                        break;
                    case Alipay.ERROR_PAY:
                        mView.showToast("支付错误:支付宝支付失败");
                        break;
                    default:
                        mView.showToast("支付错误");
                        break;
                }
            }
        }).doPay();
    }


    /**
     * 微信支付
     *
     * @param prepayId
     */
    private void doWxPay(String prepayId) {
        long timestamp = System.currentTimeMillis() / 1000;
        String nonceStr = CommonUtils.genNonceStr();
        String packageSign = "Sign=WXPay";

        Map<String, String> map = new LinkedHashMap<>();
        map.put("appid", WxConstant.WX_APP_ID);
        map.put("noncestr", nonceStr);
        map.put("package", packageSign);
        map.put("partnerid", WxConstant.PARTNER_ID);
        map.put("prepayid", prepayId);
        map.put("timestamp", String.valueOf(timestamp));

        String sign = CommonUtils.genAppSign(map);

        String payParam = CommonUtils.getJson(
                new String[]{"appid", "partnerid", "prepayid", "package", "noncestr", "timestamp", "sign"},
                new String[]{
                        WxConstant.WX_APP_ID,
                        WxConstant.PARTNER_ID,
                        prepayId,
                        packageSign,
                        nonceStr,
                        String.valueOf(timestamp),
                        sign});

        //支付前调用
        WXPay.init(MyApp.getInstance(), WxConstant.WX_APP_ID);
        WXPay.getInstance().doPay(payParam, new WXPay.WXPayResultCallBack() {
            @Override
            public void onSuccess() {
                mView.paySucess(MEMBER_WECHAT);
            }

            @Override
            public void onCancel() {
                mView.Error(MEMBER_WECHAT);
            }

            @Override
            public void onError(int errorCode) {
                mView.Error(MEMBER_WECHAT);
                switch (errorCode) {
                    case WXPay.NO_OR_LOW_WX:
                        mView.showToast("未安装微信或微信版本过低");
                        break;
                    case WXPay.ERROR_PAY_PARAM:
                        mView.showToast("参数错误");
                        break;
                    case WXPay.ERROR_PAY:
                        mView.showToast("支付失败");
                        break;
                    default:
                        break;
                }
            }
        });
    }


    public void getVipInfo() {
        biz.getVipInfo(new CallbackListener<MemberInfoBean>() {
            @Override
            public void success(MemberInfoBean object) {
                mView.onVipSucess(object);
            }

            @Override
            public void failed(Object object) {
                mView.Error(MEMBER_VIP);
            }
        });
    }
}
